/**
 * Copyright (C), 2015-2019, XXX有限公司
 * FileName: ShiroConfig
 * Author:   pc
 * Date:     2019/9/3 17:11
 * Description: shiro权限管理
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.example.rabbitmqdemo.config;



import com.example.rabbitmqdemo.filter.JwtFilter;
import com.example.rabbitmqdemo.realm.UserRealm;


import org.apache.shiro.mgt.DefaultSessionStorageEvaluator;
import org.apache.shiro.mgt.DefaultSubjectDAO;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.servlet.Filter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;


/**
 * 〈功能：〉<br> 
 * 〈shiro权限管理〉
 * @author liuhaoran
 * @create 2019/9/3 17:11
 * @since 1.0.0
 */
@Configuration
public class ShiroConfig {
	@Bean
	public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager securityManager) {
		ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
		//设置安全管理器
		shiroFilterFactoryBean.setSecurityManager(securityManager);
		//设置过滤器
		Map<String, String> filterMap = new LinkedHashMap<String, String>();
		//排除掉不需要进行认证的接口
		filterMap.put("/gologin", "anon");
		filterMap.put("/login", "anon");
		filterMap.put("/sendMsg", "anon");
		filterMap.put("/order", "anon");
		filterMap.put("/pageLisrSysLog", "anon");
		filterMap.put("/sendmail", "anon");
		// 退出系统的过滤器
		filterMap.put("/logout", "logout");
		//设置需要授权的接口
		filterMap.put("/goindex", "authc");
		filterMap.put("/add", "perms[add]");
		//设置需要认证的接口
		filterMap.put("/*", "anon");
		//跳转接口
		shiroFilterFactoryBean.setLoginUrl("/gologin");
		//授权提示接口
		shiroFilterFactoryBean.setUnauthorizedUrl("/unAuth");
		shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap);
		return shiroFilterFactoryBean;
	}

	@Bean(name = "securityManager")
	public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm) {
		DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
		securityManager.setRealm(userRealm);
		return securityManager;
	}

	@Bean(name = "UserRealm")
	public UserRealm getRealm() {
		return new UserRealm();
	}

}